package n141;

public class Solution {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode listNode = new ListNode(2);
        head.next = listNode;
        listNode.next = head;
        System.out.println(new Solution().hasCycle(head));
    }
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        // 快慢指针
        ListNode fast = head;
        ListNode slow = head;
        boolean flag = true;
        int i = 1;
        while (true) {
            for (int j = 0; j < i; j++) {
                if (fast.next == null) {
                    return false;
                }
                fast = fast.next;
                if (fast == slow) {
                    return true;
                }
            }
            slow = slow.next;
            i++;
        }
    }
}
